package com.github.cyf.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.cyf.common.util.JdbcUtil;
import com.github.cyf.entity.ActResultLog;
import com.github.cyf.mapper.ActResultLogMapper;
import com.github.cyf.service.ActResultLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.sql.*;
import java.util.List;

/**
 * @author chenyifan
 * @create 2024-04-05 18:00
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ActResultLogServiceImpl extends ServiceImpl<ActResultLogMapper, ActResultLog> implements ActResultLogService {

    private final ActResultLogMapper actResultLogMapper;
    private final JdbcUtil jdbcUtil;

    @Override
    public List<ActResultLog> pageQuery(int currentPage, int size) {
        Page<ActResultLog> page = new Page<>(currentPage, size);
        this.page(page, null);
        return page.getRecords();
    }

    @Override
    public void insertDataByJDBC(List<ActResultLog> dataList) {
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }
        long startTime = System.currentTimeMillis();
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = jdbcUtil.getConnection();
            // 手动提交事务
            conn.setAutoCommit(false);
            String sql = "insert into act_result_log2 (onlineseqid,businessid,becifno,ivisresult,createdby,createddate,updateby,updateddate,risklevel) values (?,?,?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < dataList.size(); i++) {
                ActResultLog log = dataList.get(i);
                ps.setString(1, log.getOnlineseqid());
                ps.setString(2, log.getBusinessid());
                ps.setString(3, log.getBecifno());
                ps.setString(4, log.getIvisresult());
                ps.setString(5, log.getCreatedby());
                ps.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
                ps.setString(7, log.getUpdateby());
                ps.setTimestamp(8, new Timestamp(System.currentTimeMillis()));
                ps.setString(9, log.getRisklevel());
                // 将一组参数添加到 PreparedStatement 对象的批处理命令中。
                ps.addBatch();
            }
            // 执行批处理
            ps.executeBatch();
            // 手动提交事务
            conn.commit();
            long endTime = System.currentTimeMillis();
            log.info("jdbc 批量插入数据 {} 条，用时:{}ms", dataList.size(), (endTime - startTime));
        } catch (Exception e) {
            try {
                if (conn != null) {
                    // 出现异常时进行回滚
                    conn.rollback();
                }
            } catch (SQLException ex) {
                log.error("导入数据时回滚错误", ex);
                throw new RuntimeException(ex);
            }
        } finally {
            jdbcUtil.close(conn, ps);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertDataByMybatis(List<ActResultLog> dataList) {
        long startTime = System.currentTimeMillis();
        actResultLogMapper.executeInsert(dataList);
        long endTime = System.currentTimeMillis();
        log.info("mybatis 批量插入数据 {} 条，用时:{}ms", dataList.size(), (endTime - startTime));
    }

}
